<?php

/**
 *Simple CRUD Code Generator
 *Email:  support@crazedcoders.com
 *Website: http://www.crazedcoders.com/
 *Copyright (C) 2007 CrazedCoders
 *
 *This program is free software: you can redistribute it and/or modify
 *it under the terms of the GNU General Public License as published by
 *the Free Software Foundation, either version 3 of the License, or
 *(at your option) any later version.
 *
 *THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *SOFTWARE.
 *
 *You should have received a copy of the GNU General Public License
 *along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * generates a Cairngorm command for a given table
 */
function generateAppController( $tables ) {

	$export =
'package '.con::$package.'.control
{
	/**
	 *Simple CRUD Code Generator
	 *Email:  support@crazedcoders.com
	 *Website: http://www.crazedcoders.com/
	 *Copyright (C) 2007 CrazedCoders
	 *
	 *This program is free software: you can redistribute it and/or modify
	 *it under the terms of the GNU General Public License as published by
	 *the Free Software Foundation, either version 3 of the License, or
	 *(at your option) any later version.
	 *
	 *THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	 *SOFTWARE.
	 *
	 *You should have received a copy of the GNU General Public License
	 *along with this program.  If not, see <http://www.gnu.org/licenses/>.
 	 */

	/*****************************************************************
	 * WARNING: file is generated and may be over written
	 * SOLUTION: store custom code in the _include files
	 *****************************************************************/
	import com.adobe.cairngorm.control.FrontController;
	import '.con::$package.'.command.*;
	import '.con::$package.'.event.*;
	import '.con::$package.'.vo.*;

	public class AppController extends FrontController
	{
		public function AppController()
		{
';


	foreach( $tables as $tableName=>$table ) {
		if( ! array_key_exists( 'isManyToMany', $table )
				|| $table['isManyToMany']
			){
			continue;
		}

		//$tableName = $tables[$k][0]['table_name'];
		$className = toCamelCase( $tableName );
		$classNameUp = strtoupper( $className );
		$classNameLow = strtolower( $className );
		$columns = getTableColumns( $tableName );
		$columnsString = join(', ',array_keys($columns));
		//$primaryKey = toCamelCaseMember($assoc['column_name']);
		if( $table[0][2]['constraint_type'] == 'PRIMARY KEY' ) {
			$primaryKey = toCamelCaseMember(
					$table[0][2]['column_name'] );
		}
		$secondaryKeys = array();
		foreach( $columns as $key=>$value ) {
			$secondaryKeys[$key] = $columns[$key];
		}
		//$secondaryKeys = StdFunc::array_clone( $columns );
		if( isset ( $primaryKey ) ) {
			unset( $secondaryKeys[$primaryKey] );
		}
		$export .=

'
			addCommand('.$className.'Event.'.$classNameUp.'_ACTION_COUNT_ALL, '.$className.'Command );
			addCommand('.$className.'Event.'.$classNameUp.'_ACTION_GET_ALL, '.$className.'Command );
			addCommand('.$className.'Event.'.$classNameUp.'_ACTION_GET_ALL_BY_START_LENGTH,'.$className.'Command );
			addCommand('.$className.'Event.'.$classNameUp.'_ACTION_CREATE_ONE, '.$className.'Command );
			addCommand('.$className.'Event.'.$classNameUp.'_ACTION_DELETE_ONE, '.$className.'Command );
			addCommand('.$className.'Event.'.$classNameUp.'_ACTION_UPDATE_ONE, '.$className.'Command );
			addCommand('.$className.'Event.'.$classNameUp.'_ACTION_GET_ONE, '.$className.'Command );
';
	}

	$export .=
'

		}
	}
}
';

	return $export;

}


?>
